package com.simple.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 获取bean
 * @author wangxing
 *
 */
@Component
public final class SpringBeanUtil implements ApplicationContextAware {

	private static ApplicationContext conext;

	public static Object getBean(String id) {
		if (conext == null) {
			throw new NullPointerException("ApplicationContext is null");
		}
		return conext.getBean(id);
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T bean(String id) {
		if (conext == null) 
			throw new NullPointerException("ApplicationContext is null");
		return (T)conext.getBean(id);
	}

	public static <T> T getBean(Class<T> requiredType) {
		return conext.getBean(requiredType);
	}
	
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		conext = applicationContext;		
	}

}